package com.sc.dao.impl;

import com.sc.dao.OrderDao;
import com.sc.entity.Order;
import com.sc.utils.JDBCUtils;

import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public int add(Order order) throws Exception {
        String sql= "insert into order1(id,user_id,total_price,status)values(?,?,?,?)";
        return JDBCUtils.update(sql,new Object[]{order.getId(),order.getUserId(),order.getTotalPrice(),
        order.getStatus()});

    }

    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from order1 where id = ?";
        return JDBCUtils.update(sql,new Object[]{id});

    }
    @Override
    public int update(Order order) throws Exception {
        String sql = "update order1 set user_id = ? ,total_price =?," +
                "status = ? ";
        return JDBCUtils.update(sql,new Object[]{order.getUserId(),order.getTotalPrice(),
        order.getStatus()});
    }

    @Override
    public Order queryById(int id) throws Exception {
        String sql = "select from order1 where id = ?";
        return JDBCUtils.query(sql,Order.class,new Object[id]);

    }

    @Override
    public List<Order> queryAll() throws Exception {
        String sql = "select from order1";
        return JDBCUtils.querylist(sql,Order.class,new Object[0]);

    }

    @Override
    public Order findByUserIdAndLast(Integer id) throws Exception {
        String sql = "select * from 'order1' where user_id = 1 order1 by create_time desc limit 1";
       return JDBCUtils.query(sql,Order.class,new Object[id]);
    }
}
